home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / Tickle-4.0 (tcl) / tcl / tests / file.test < prev    next >
Encoding:
Text File  |  1993-11-02  |  11.8 KB  |  320 lines  |  [TEXT/MPS ]

  1. # Commands covered:  file
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1993 The Regents of the University of California.
  8. # All rights reserved.
  9. #
  10. # Permission is hereby granted, without written agreement and without
  11. # license or royalty fees, to use, copy, modify, and distribute this
  12. # software and its documentation for any purpose, provided that the
  13. # above copyright notice and the following two paragraphs appear in
  14. # all copies of this software.
  15. #
  16. # IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  17. # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  18. # OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  19. # CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. #
  21. # THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  22. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23. # AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  24. # ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  25. # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  26. #
  27. # $Header: /user6/ouster/tcl/tests/RCS/file.test,v 1.22 93/04/16 16:46:42 ouster Exp $ (Berkeley)
  28.  
  29. if {[string compare test [info procs test]] == 1} then {source defs}
  30.  
  31. # rootname and ext
  32.  
  33. test file-1.1 {rootname and extension options} {file ext abc.def} .def
  34. test file-1.2 {rootname and extension options} {file ro abc.def} abc
  35. test file-1.3 {rootname and extension options} {file extension a:b:c.d} .d
  36. test file-1.4 {rootname and extension options} {file rootname a:b:c.d} a:b:c
  37. test file-1.5 {rootname and extension options} {file extension a:b.c:d} {}
  38. test file-1.6 {rootname and extension options} {file rootname a:b.c:d} a:b.c:d
  39. set num 7
  40. foreach outer { {} a .a a. a.a } {
  41.   foreach inner { {} a .a a. a.a } {
  42.     set thing [format %s:%s $outer $inner]
  43.     test file-1.$num {rootname and extension options} {
  44.     format %s%s [file rootname $thing] [file ext $thing]
  45.     } $thing
  46.     set num [expr $num+1]
  47.   }
  48. }
  49.  
  50. # dirname and tail
  51.  
  52. test file-2.1 {dirname and tail options} {file dirname .def} {}
  53. test file-2.2 {dirname and tail options} {file tail abc.def} abc.def
  54. test file-2.3 {dirname and tail options} {file d a:b:c.d} a:b
  55. test file-2.4 {dirname and tail options} {file ta a:b:c.d} c.d
  56. test file-2.5 {dirname and tail options} {file dirname a:b.c:d} a:b.c
  57. test file-2.6 {dirname and tail options} {file tail a:b.c:d} d
  58. set num 7
  59. foreach outer { a .a a. a.a } {
  60.   foreach inner { {} a .a a. a.a } {
  61.     set thing [format %s:%s $outer $inner]
  62.     test file-2.$num {dirname and tail options} {
  63.     format %s:%s [file dirname $thing] [file tail $thing]
  64.     } $thing
  65.     set num [expr $num+1]
  66.   }
  67. }
  68.  
  69. # exists
  70.  
  71. catch {rm -f :dir.file:gorp.file}
  72. catch {rm -f :gorp.file}
  73. catch {rmdir :dir.file}
  74.  
  75. test file-3.1 {exists option} {file exists gorp.file} 0
  76. test file-3.2 {exists option} {file exists :dir.file:gorp.file} 0
  77.  
  78. set fd [open gorp.file "w"]
  79. puts $fd "abcde"
  80. close $fd
  81.  
  82. catch {mkdir dir.file}
  83.  
  84. set fd [open :dir.file:gorp.file "w"]
  85. puts $fd "12345"
  86. close $fd
  87.  
  88. test file-3.3 {exists option} {file exists gorp.file} 1
  89. test file-3.4 {exists option} {file exi :dir.file:gorp.file} 1
  90.  
  91. # executable
  92.  
  93. test file-4.2 {executable option} {file exe $env(APPDIR):$env(APPNAME)} 1
  94.  
  95. # isdirectory
  96.  
  97. test file-5.1 {isdirectory option} {file isdirectory gorp.file} 0
  98. test file-5.2 {isdirectory option} {file isd dir.file} 1
  99.  
  100. # isfile
  101.  
  102. test file-6.1 {isfile option} {file isfile gorp.file} 1
  103. test file-6.2 {isfile option} {file isfile dir.file} 0
  104.  
  105. # isowned
  106.  
  107. test file-7.1 {owned option} {file owned gorp.file} 1
  108. #if {$user != "root"} {
  109. #    test file-7.2 {owned option} {file owned /} 0
  110. #}
  111.  
  112. # readable
  113.  
  114. #exec chmod 444 gorp.file
  115. test file-8.1 {readable option} {file readable gorp.file} 1
  116. #exec chmod 333 gorp.file
  117. #if {$user != "root"} {
  118. #    test file-8.2 {readable option} {file reada gorp.file} 0
  119. #}
  120.  
  121. # writable
  122.  
  123. #exec chmod 555 gorp.file
  124. #if {$user != "root"} {
  125. #    test file-9.1 {writable option} {file writable gorp.file} 0
  126. #}
  127. #exec chmod 222 gorp.file
  128. test file-9.2 {writable option} {file w gorp.file} 1
  129.  
  130. return ""
  131.  
  132. ###
  133. ### End of modifications...
  134. ### UNDONE
  135. ###
  136.  
  137. # stat
  138.  
  139. exec cat > gorp.file << "Test string"
  140. exec chmod 765 gorp.file
  141. test file-10.1 {stat option} {
  142.     catch {unset stat}
  143.     file stat gorp.file stat
  144.     lsort [array names stat]
  145. } {atime ctime dev gid ino mode mtime nlink size type uid}
  146. test file-10.2 {stat option} {
  147.     catch {unset stat}
  148.     file stat gorp.file stat
  149.     list $stat(nlink) $stat(size) [expr $stat(mode)&0777] $stat(type)
  150. } {1 11 501 file}
  151. test file-10.3 {stat option} {
  152.     string tolower [list [catch {file stat _bogus_ stat} msg] \
  153.         $msg $errorCode]
  154. } {1 {couldn't stat "_bogus_": no such file or directory} {posix enoent {no such file or directory}}}
  155. test file-10.4 {stat option} {
  156.     list [catch {file stat _bogus_} msg] $msg $errorCode
  157. } {1 {wrong # args: should be "file stat name varName"} NONE}
  158. test file-10.5 {stat option} {
  159.     list [catch {file stat _bogus_ a b} msg] $msg $errorCode
  160. } {1 {wrong # args: should be "file stat name varName"} NONE}
  161. test file-10.6 {stat option} {
  162.     catch {unset x}
  163.     set x 44
  164.     list [catch {file stat gorp.file x} msg] $msg $errorCode
  165. } {1 {can't set "x(dev)": variable isn't array} NONE}
  166. catch {unset stat}
  167.  
  168. # mtime, and size (I've given up trying to find a test for "atime":  there
  169. # seem to be too many quirks in the way file systems handle this to come
  170. # up with a reproducible test).
  171.  
  172. test file-11.1 {mtime and atime and size options} {
  173.     catch {unset stat}
  174.     file stat gorp.file stat
  175.     list [expr {[file mtime gorp.file] == $stat(mtime)}] \
  176.         [expr {[file atime gorp.file] == $stat(atime)}] \
  177.         [file size gorp.file]
  178. } {1 1 11}
  179. test file-11.2 {mtime option} {
  180.     set old [file mtime gorp.file]
  181.     exec sleep 2
  182.     set f [open gorp.file w]
  183.     puts $f "More text"
  184.     close $f
  185.     set new [file mtime gorp.file]
  186.     expr {($new > $old) && ($new <= ($old+5))}
  187. } {1}
  188. test file-11.3 {size option} {
  189.     set oldsize [file size gorp.file]
  190.     set f [open gorp.file a]
  191.     puts $f "More text"
  192.     close $f
  193.     expr {[file size gorp.file] - $oldsize}
  194. } {10}
  195. test file-11.4 {errors in atime option} {
  196.     list [catch {file atime _bogus_ x} msg] $msg $errorCode
  197. } {1 {wrong # args: should be "file atime name"} NONE}
  198. test file-11.5 {errors in atime option} {
  199.     string tolower [list [catch {file atime _bogus_} msg] \
  200.         $msg $errorCode]
  201. } {1 {couldn't stat "_bogus_": no such file or directory} {posix enoent {no such file or directory}}}
  202. test file-11.6 {errors in mtime option} {
  203.     list [catch {file mtime _bogus_ x} msg] $msg $errorCode
  204. } {1 {wrong # args: should be "file mtime name"} NONE}
  205. test file-11.7 {errors in mtime option} {
  206.     string tolower [list [catch {file mtime _bogus_} msg] $msg \
  207.         $errorCode]
  208. } {1 {couldn't stat "_bogus_": no such file or directory} {posix enoent {no such file or directory}}}
  209. test file-11.8 {errors in size option} {
  210.     list [catch {file size _bogus_ x} msg] $msg $errorCode
  211. } {1 {wrong # args: should be "file size name"} NONE}
  212. test file-11.9 {errors in size option} {
  213.     string tolower [list [catch {file size _bogus_} msg] $msg \
  214.         $errorCode]
  215. } {1 {couldn't stat "_bogus_": no such file or directory} {posix enoent {no such file or directory}}}
  216.  
  217. # type
  218.  
  219. test file-12.1 {type option} {
  220.     file type dir.file
  221. } directory
  222. test file-12.2 {type option} {
  223.     file type gorp.file
  224. } file
  225. if $atBerkeley {
  226.     exec ln -s a/b/c link.file
  227.     test file-12.3 {type option} {
  228.     file type link.file
  229.     } link
  230.     exec rm link.file
  231. }
  232. test file-12.4 {errors in type option} {
  233.     list [catch {file type a b} msg] $msg $errorCode
  234. } {1 {wrong # args: should be "file type name"} NONE}
  235. test file-12.5 {errors in type option} {
  236.     string tolower [list [catch {file type _bogus_} msg] $msg $errorCode]
  237. } {1 {couldn't stat "_bogus_": no such file or directory} {posix enoent {no such file or directory}}}
  238.  
  239. # lstat and readlink:  run these tests only at Berkeley, since not all
  240. # sites will have symbolic links
  241.  
  242. if $atBerkeley {
  243.     exec ln -s gorp.file link.file
  244.     test file-13.1 {lstat option} {
  245.     catch {unset stat}
  246.     file lstat link.file stat
  247.     lsort [array names stat]
  248.     } {atime ctime dev gid ino mode mtime nlink size type uid}
  249.     test file-13.1 {lstat option} {
  250.     catch {unset stat}
  251.     file lstat link.file stat
  252.     list $stat(nlink) [expr $stat(mode)&0777] $stat(type)
  253.     } {1 511 link}
  254.     test file-13.3 {errors in lstat option} {
  255.     string tolower [list [catch {file lstat _bogus_ stat} msg] \
  256.         $msg $errorCode]
  257.     } {1 {couldn't lstat "_bogus_": no such file or directory} {posix enoent {no such file or directory}}}
  258.     test file-13.4 {errors in lstat option} {
  259.     list [catch {file lstat _bogus_} msg] $msg $errorCode
  260.     } {1 {wrong # args: should be "file lstat name varName"} NONE}
  261.     test file-13.5 {errors in lstat option} {
  262.     list [catch {file lstat _bogus_ a b} msg] $msg $errorCode
  263.     } {1 {wrong # args: should be "file lstat name varName"} NONE}
  264.     test file-13.6 {errors in lstat option} {
  265.     catch {unset x}
  266.     set x 44
  267.     list [catch {file lstat gorp.file x} msg] $msg $errorCode
  268.     } {1 {can't set "x(dev)": variable isn't array} NONE}
  269.     catch {unset stat}
  270.  
  271.     test file-14.1 {readlink option} {
  272.     file readlink link.file
  273.     } gorp.file
  274.     test file-14.2 {errors in readlink option} {
  275.     list [catch {file readlink a b} msg] $msg $errorCode
  276.     } {1 {wrong # args: should be "file readlink name"} NONE}
  277.     test file-14.3 {errors in readlink option} {
  278.     list [catch {file readlink _bogus_} msg] $msg $errorCode
  279.     } {1 {couldn't readlink "_bogus_": no such file or directory} {POSIX ENOENT {no such file or directory}}}
  280.  
  281.     exec rm link.file
  282. }
  283.  
  284. # Error conditions
  285.  
  286. test file-15.1 {error conditions} {
  287.     list [catch file msg] $msg
  288. } {1 {wrong # args: should be "file option name ?arg ...?"}}
  289. test file-15.2 {error conditions} {
  290.     list [catch {file x} msg] $msg
  291. } {1 {wrong # args: should be "file option name ?arg ...?"}}
  292. test file-15.3 {error conditions} {
  293.     list [catch {file exists x too} msg] $msg
  294. } {1 {wrong # args: should be "file exists name"}}
  295. test file-15.4 {error conditions} {
  296.     list [catch {file gorp x} msg] $msg
  297. } {1 {bad option "gorp": should be atime, dirname, executable, exists, extension, isdirectory, isfile, lstat, mtime, owned, readable, readlink, root, size, stat, tail, type, or writable}}
  298. test file-15.5 {error conditions} {
  299.     list [catch {file ex x} msg] $msg
  300. } {1 {bad option "ex": should be atime, dirname, executable, exists, extension, isdirectory, isfile, lstat, mtime, owned, readable, readlink, root, size, stat, tail, type, or writable}}
  301. test file-15.6 {error conditions} {
  302.     list [catch {file is x} msg] $msg
  303. } {1 {bad option "is": should be atime, dirname, executable, exists, extension, isdirectory, isfile, lstat, mtime, owned, readable, readlink, root, size, stat, tail, type, or writable}}
  304. test file-15.7 {error conditions} {
  305.     list [catch {file read x} msg] $msg
  306. } {1 {bad option "read": should be atime, dirname, executable, exists, extension, isdirectory, isfile, lstat, mtime, owned, readable, readlink, root, size, stat, tail, type, or writable}}
  307. test file-15.8 {error conditions} {
  308.     list [catch {file s x} msg] $msg
  309. } {1 {bad option "s": should be atime, dirname, executable, exists, extension, isdirectory, isfile, lstat, mtime, owned, readable, readlink, root, size, stat, tail, type, or writable}}
  310. test file-15.9 {error conditions} {
  311.     list [catch {file t x} msg] $msg
  312. } {1 {bad option "t": should be atime, dirname, executable, exists, extension, isdirectory, isfile, lstat, mtime, owned, readable, readlink, root, size, stat, tail, type, or writable}}
  313. test file-15.10 {error conditions} {
  314.     list [catch {file rootname ~woohgy} msg] $msg
  315. } {1 {user "woohgy" doesn't exist}}
  316.  
  317. exec chmod 777 dir.file
  318. exec rm dir.file/gorp.file gorp.file
  319. exec rmdir dir.file
  320.